package algorithm.easy;

public class Solution141 {

    //输入：head = [3,2,0,-4], pos = 1
    //输出：true
    //解释：链表中有一个环，其尾部连接到第二个节点。
    public static boolean hasCycle(ListNode head) {
        boolean flag = false;
        if(head==null||head.next==null){
            return flag;
        }
        ListNode slow = head;
        ListNode fast = head.next.next;
        while(slow!=null && fast!=null){
            if(slow == fast){
                flag = true;
                return flag;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return flag;
    }

    public static void main(String[] args){
        ListNode head = ListNode.init141();
        System.out.println(hasCycle(head));

    }
}
